package com.xiaoyu.izf.controller;

import cn.hutool.core.lang.Validator;
import com.alibaba.fastjson.JSON;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.page.FormResult;
import com.xiaoyu.izf.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/reg_page")
    public String regPage(String pushCode, Model model){
        model.addAttribute("code",pushCode);
        return "/user/reg";
    }

//    @RequestMapping("/reg_page/{pushCode}")
//    public String regPage(@PathVariable("pushCode") String pushCode , Model model){
//        model.addAttribute("code",pushCode);
//        return "/user/reg";
//    }

    @RequestMapping("/reg")
    @ResponseBody
    public String reg(HttpServletRequest request,String userName,String nickName,String pwd,String pushCode,String smscode){
        FormResult<UserInfo> fr = new FormResult<UserInfo>();
        if(!Validator.isMobile(userName)){
            fr.setStatus(500);
            fr.setMessage("手机号码不正确。");
            return "<script>parent.callBack("+ JSON.toJSONString(fr)+");</script>";
        }
        String code = (String)request.getSession().getAttribute("msgCodeSessinId");
        if(StringUtils.isEmpty(code)){
            fr.setStatus(500);
            fr.setMessage("手机验证码已过期。");
            return "<script>parent.callBack("+ JSON.toJSONString(fr)+");</script>";
        }else{
            if(!code.equals(smscode)){
                fr.setStatus(500);
                fr.setMessage("手机验证码错误。");
                return "<script>parent.callBack("+ JSON.toJSONString(fr)+");</script>";
            }
        }
        if(StringUtils.isEmpty(nickName)){
            fr.setStatus(500);
            fr.setMessage("用户昵称不能为空。");
            return "<script>parent.callBack("+ JSON.toJSONString(fr)+");</script>";
        }
        if(StringUtils.isEmpty(pwd)){
            fr.setStatus(500);
            fr.setMessage("密码不能为空。");
            return "<script>parent.callBack("+ JSON.toJSONString(fr)+");</script>";
        }
        UserInfo user = userInfoService.findByUserName(userName);
        if(user!=null){
            fr.setStatus(500);
            fr.setMessage("用户已经存在。");
            return "<script>parent.callBack("+ JSON.toJSONString(fr)+");</script>";
        }
        UserInfo pushUser = null;
        if(!StringUtils.isEmpty(pushCode)){
            pushUser = userInfoService.userInfoByPushCode(pushCode);
            if(pushUser==null){
                fr.setStatus(500);
                fr.setMessage("推广码错误。");
                return "<script>parent.callBack("+ JSON.toJSONString(fr)+");</script>";
            }
            if(pushUser.getVipType()==0){
                fr.setStatus(500);
                fr.setMessage("推荐人不是合伙人，无推广资格。");
                return "<script>parent.callBack("+ JSON.toJSONString(fr)+");</script>";
            }
        }
        user = userInfoService.userInfoAdd(userName,nickName,pwd,pushUser);
        fr.setStatus(200);
        fr.setMessage("ok");
        return "<script>parent.callBack("+ JSON.toJSONString(fr)+");</script>";
    }

    @RequestMapping("/down_page")
    public String downPage(HttpServletRequest request){
        return "/user/down_page";
    }

    @RequestMapping("/down")
    public String downPage2(HttpServletRequest request){
        return "/user/down";
    }
}
